class Solution
{
public:
    int minOperationsMaxProfit(vector<int> &customers, int boardingCost, int runningCost)
    {
        int profit = 0;
        int maxProfit = 0;
        int curCustomer = 0;
        int opTime = 0;
        int maxOpTime = 0;
        for (int customer : customers)
        {
            ++opTime;
            curCustomer += customer;
            int boardCustomer = min(curCustomer, 4);
            curCustomer -= boardCustomer;
            profit += boardingCost * boardCustomer;
            profit -= runningCost;
            if (profit > maxProfit)
            {
                maxProfit = profit;
                maxOpTime = opTime;
            }
        }
        while (curCustomer > 0)
        {
            ++opTime;
            int boardCustomer = min(curCustomer, 4);
            curCustomer -= boardCustomer;
            profit += boardingCost * boardCustomer;
            profit -= runningCost;
            if (profit > maxProfit)
            {
                maxProfit = profit;
                maxOpTime = opTime;
            }
        }
        return maxProfit > 0 ? maxOpTime : -1;
    }
};